/*
 * GenericDAO.java
 *
 * Created on July 28, 2005, 10:33 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.src.computec.radar.base.dao;

import com.src.computec.radar.base.dao.exception.DAOException;
import java.sql.*;
import javax.sql.*;
import java.io.Serializable;
import com.src.computec.radar.base.servicelocator.ServiceLocator;
import com.src.computec.radar.base.servicelocator.exception.ServiceLocatorException;
import com.src.computec.radar.base.constantes.ConstantesGenerales;
import java.math.BigDecimal;

/**
 * Clase base para los dao u objetos que realizaran las consultas a la base
 * de datos.
 * Contiene una serie de metodos utilitarios, disponibles para quienes
 * extiendan de esta clase
 * <p>Title: Generador de Contenido XHTML</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: SRC - APISSCOR</p>
 * @author SRC SOLUCIONES & CIA
 * @version 1.0
 */
public class GenericDAO implements Serializable {
    
    public static final String AUD_INSERTAR_LBL = "Insertar";
    public static final String AUD_ACTUALIZAR_LBL = "Actualizar";
    public static final String AUD_ELIMINAR_LBL = "Eliminar"; 
    
    protected GenericDAO() {
    }

    public Connection getConnectionDB2() throws DAOException {
        Connection conn = null;
        try {
            ServiceLocator sl = ServiceLocator.getInstance();
            DataSource ds = sl.getDataSource(ConstantesGenerales.getInstance().JNDINAME_DB2, true);
            conn = ds.getConnection();
            conn.setAutoCommit(true);
        }
        catch (SQLException ex) {
            throw new DAOException("GenericDAO:getConnectionDB2:" + ex.getMessage(), ex);
        }
        catch (ServiceLocatorException ex) {
            throw new DAOException("GenericDAO:getConnectionDB2:" + ex.getMessage(), ex);
        }       
        
        return conn;
    }

    public Connection getConnectionDB2XA() throws DAOException {
        Connection conn = null;
        try {
            ServiceLocator sl = ServiceLocator.getInstance();
            DataSource ds = sl.getDataSource(ConstantesGenerales.getInstance().JNDINAME_DB2_XA, true);
            conn = ds.getConnection();
        }
        catch (SQLException ex) {
            throw new DAOException("GenericDAO:getConnectionDB2XA:" + ex.getMessage(), ex);
        }
        catch (ServiceLocatorException ex) {
            throw new DAOException("GenericDAO:getConnectionDB2XA:" + ex.getMessage(), ex);
        }
        return conn;
    }

    public Connection getConnectionSqlServer() throws DAOException {
        Connection conn = null;
        try {
            ServiceLocator sl = ServiceLocator.getInstance();
            DataSource ds = sl.getDataSource(ConstantesGenerales.getInstance().JNDINAME_SQLSERVER, true);
            conn = ds.getConnection();        
        }
        catch (SQLException ex) {
            throw new DAOException("GenericDAO:getConnectionSqlServer:" + ex.getMessage(), ex);
        }
        catch (ServiceLocatorException ex) {
            throw new DAOException("GenericDAO:getConnectionSqlServer:" + ex.getMessage(), ex);
        }
        return conn;
    }

    public Connection getConnectionSqlServerXA() throws DAOException {
        Connection conn = null;
        try {
            ServiceLocator sl = ServiceLocator.getInstance();
            DataSource ds = sl.getDataSource(ConstantesGenerales.getInstance().JNDINAME_SQLSERVER_XA, true);
            conn = ds.getConnection();        
        }
        catch (SQLException ex) {
            throw new DAOException("GenericDAO:getConnectionSqlServerXA:" + ex.getMessage(), ex);
        }
        catch (ServiceLocatorException ex) {
            throw new DAOException("GenericDAO:getConnectionSqlServerXA:" + ex.getMessage(), ex);
        }
        return conn;
    }

    public void closeConnection(Connection connection, PreparedStatement preparedStatement, ResultSet rs) throws DAOException {
        try {
            if (rs != null) {
                rs.close();
            }
        }
        catch (SQLException e) {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
        catch (SQLException e) {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }
        try {
            if (connection != null) {                
                connection.close();
            }
        }
        catch (SQLException e) {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }
    }

    public void closeConnection(PreparedStatement preparedStatement, ResultSet rs) throws DAOException {
        try {
            if (rs != null) {
                rs.close();
            }
        }
        catch (SQLException e) {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
        catch (SQLException e) {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }
    }

    public void closeConnection(Connection connection, PreparedStatement preparedStatement) throws DAOException 
    {
        try 
        {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
        catch (SQLException e) 
        {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }
        
        try 
        {
            if (connection != null) 
            {
                connection.close();
            }
        }
        catch (SQLException e) {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }    
    }

    public void closeConnection(Connection connection) throws DAOException {
        try {
            if (connection != null) {
                connection.close();
            }
        }
        catch (SQLException e) {
            throw new DAOException("GenericDAO:closeConnection " + e.getMessage(), e);
        }
    }

    synchronized public String getNextId(Connection connection, String tabla, String campo) throws DAOException {
        PreparedStatement statement = null;
        String sql = "";
        ResultSet rs = null;
        BigDecimal id = null;
        try {
            sql = "SELECT MAX(" + campo + ") FROM " + tabla;
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            if (rs.next()) {
                int indice1 = 0;
                String maxTable = rs.getString(++indice1);
                id = new BigDecimal(maxTable != null ? maxTable :"0");
            }
            if (id == null) {
                id = new BigDecimal("0");
            }
            id = id.add(BigDecimal.ONE);
            return id.toString();
        }
        catch (SQLException e) {
            throw new DAOException("Error en getNextId. " + e.getMessage(), e);
        }
        finally {
            closeConnection(statement, rs);
        }
    }
}